import logging
import os.path
#默认日志目录
from utils import cur_path

LOGDIR = "logs"
#默认日志等级
LOGLEVEL = logging.INFO

import os
import sys

import traceback
from cmd import Cmd
from command import xtermCMD
import time

import shutil


IP = "10.0.0.0/8"
PORT = 9000


P2PNet=None
MODE = "pow"

def setMode(mode):
    global MODE;
    MODE = mode;

def setupP2PNet(arg1=1, arg2=3, netType="net", attack="none",attack_num=1):

    global P2PNet;

    P2PNet = {
        "IP": [
            "192.168.14.132",
            "192.168.14.143",
            "192.168.14.146",
            "192.168.14.147",
        ]
    }

    path = os.path.join(cur_path, 'CMD')
    with open(os.path.join(path,"main"), 'w') as f:
        f.close()


class myCommand(Cmd):
    intro = "Control the bitcoin simulation network. Type help or ? to list commands.\n"
    prompt = ">>> "

    def do_EXIT(self):
        os.system("killall -SIGKILL xterm")
        os.system("mn --clean > /dev/null 2>&1")

    def do_ShowNodes(self, line):
        print("showNode")

    def do_AddNode(self, line):
        print("addNode")

    def do_DelNode(self, line):
        print("deleteNode")

    def do_Execute(self,line):
        print("Execute")

    def do_CLI(self,Line):
        print("cli")


def delete_log():
    log_path = os.path.join(cur_path,'Logs')
    if os.path.exists(log_path):
        shutil.rmtree(log_path)
    if not os.path.exists(log_path):
        os.mkdir(log_path)

def deleteCMD():
    CMDpath = os.path.join(cur_path,'CMD')
    if os.path.exists(CMDpath):
        shutil.rmtree(CMDpath)
    if not os.path.exists(CMDpath):
        os.mkdir(CMDpath)


if __name__ == '__main__':

    try:
        delete_log();
        deleteCMD();
        setupP2PNet(5,1,netType='star');
        myCommand().cmdloop();
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        traceback.print_stack()

        os.system("killall -SIGKILL xterm")
        os.system("mn --clean > /dev/null 2>&1")

